<template>
  <div class="contract-box">
    <div class="add-contract-box">
      <div class="contract-head">新建合同</div>
      <div class="box-contract-info">
        <div style="width: 200px">合同基本信息</div>
        <div class="basic-info">
          <span>合同名称:</span>
          <el-input class="my_style" />
        </div>
        <div class="basic-info">
          <span>合同编号:</span>
          <el-input class="my_style" readonly value="系统自动生成" />
        </div>
        <div class="basic-info">
          <span>&nbsp;&nbsp;&nbsp;&nbsp;类型:</span>
          <el-input class="my_style" />
        </div>
        <div class="basic-info">
          <span>&nbsp;&nbsp;&nbsp;&nbsp;密级:</span>
          <el-input class="my_style" />
        </div>
        <div class="time-box">
          <span>交付日期:</span>
          <el-date-picker
            type="date"
            placeholder="选择日期"
            style="margin-left: 20px"
          >
          </el-date-picker>
        </div>
        <div class="basic-info">
          <span>合同金额:</span>
          <el-input class="my_style" />
        </div>
        <div class="basic-info">
          <span>合同说明:</span>
          <el-input type="textarea" :rows="3" class="textarea-sty"></el-input>
        </div>
      </div>

      <!--相关项目信息-->
      <div class="box-bind-info">
        <div style="width: 200px">相关项目信息</div>
        <div class="basic-info">
          <span>项目名称:</span>
          <el-input class="my_style" />
        </div>
        <div class="basic-info">
          <span>项目编号:</span>
          <el-input class="my_style" />
        </div>
        <div class="basic-info">
          <span>招标方式:</span>
          <el-input class="my_style" />
        </div>
        <div class="basic-info">
          <span>业务类型:</span>
          <el-input class="my_style" />
        </div>
      </div>
      <!--合同清单-->
      <div class="box-product-info">
        <div style="width: 200px">合同清单</div>
        <div style="width: 1200px; margin: 20px auto">
          <el-table :data="planInfoList" style="width: 100%" border>
            <el-table-column type="expand">
              <template slot-scope="props">
                <el-form label-position="left" inline class="demo-table-expand">
                  <el-form-item label="税率" style="margin-right: 80px">
                    <span>{{ props.row.taxRate + "%" }}</span>
                  </el-form-item>
                  <el-form-item label="预算总价" style="margin-right: 80px">
                    <span>{{ props.row.bdgTotlePrice + "元" }}</span>
                  </el-form-item>
                  <el-form-item label="交付时间" style="margin-right: 80px">
                    <span>{{ props.row.delvTime }}</span>
                  </el-form-item>
                  <el-form-item label="需求说明" style="margin-right: 80px">
                    <span>{{ props.row.prodExplain }}</span>
                  </el-form-item>
                  <el-form-item label="采购人">
                    <span>{{ props.row.purchaser }}</span>
                  </el-form-item>
                </el-form>
              </template>
            </el-table-column>
            <el-table-column
              label="预算编号"
              align="center"
              prop="bgId"
              width="200"
            />
            <el-table-column
              label="产品编号"
              align="center"
              prop="prodId"
              width="200"
            />
            <el-table-column
              label="产品名称"
              align="center"
              prop="prodName"
              width="200"
            />
            <el-table-column
              label="规格"
              align="center"
              prop="size"
              width="150"
            />
            <el-table-column
              label="预算单价"
              align="center"
              prop="bdgSinglePrice"
              width="140"
            />
            <el-table-column label="需求数量" align="center" prop="reqNumber" />
          </el-table>
          <pagination
            v-show="planInfoTotal > 0"
            :total="planInfoTotal"
            :page.sync="planInfoqueryParams.pageNum"
            :limit.sync="planInfoqueryParams.pageSize"
            @pagination="getPlanInfoList"
          />
          <div>&nbsp;</div>
        </div>
      </div>
      <!--相关项目信息-->
      <div class="box-bind-info">
        <div style="width: 200px">合同履行约定</div>
        <div>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['basic:agree:add']"
                >新增</el-button
              >
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['basic:agree:edit']"
                >修改</el-button
              >
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['basic:agree:remove']"
                >删除</el-button
              >
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPermi="['basic:agree:export']"
                >导出</el-button
              >
            </el-col>
            <right-toolbar
              :showSearch.sync="showSearch"
              @queryTable="getList"
            ></right-toolbar>
          </el-row>

          <el-table
            v-loading="loading"
            :data="agreeList"
            @selection-change="handleSelectionChange"
            border
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="款项类型" align="center" prop="paymentType">
              <template slot-scope="scope">
                <dict-tag
                  :options="dict.type.bs_payment_type"
                  :value="scope.row.paymentType"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="付款单位"
              align="center"
              prop="paymentUnit"
            />
            <el-table-column
              label="付款方式"
              align="center"
              prop="paymentMethod"
            >
              <template slot-scope="scope">
                <dict-tag
                  :options="dict.type.bs_payment_method"
                  :value="scope.row.paymentMethod"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="付款日期"
              align="center"
              prop="paymentTime"
              width="180"
            >
              <template slot-scope="scope">
                <span>{{
                  parseTime(scope.row.paymentTime, "{y}-{m}-{d}")
                }}</span>
              </template>
            </el-table-column>

            <el-table-column label="收款单位" align="center" prop="supName" />

            <el-table-column
              label="付款金额"
              align="center"
              prop="paymentMoney"
            />
            <el-table-column label="违约责任" align="center" prop="backOut" />

            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['basic:agree:edit']"
                  >修改</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['basic:agree:remove']"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </div>
    </div>

    <!-- 添加或修改合同约定对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="款项类型" prop="paymentType">
          <el-select v-model="form.paymentType" placeholder="请选择款项类型">
            <el-option
              v-for="dict in dict.type.bs_payment_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付款日期" prop="paymentTime">
          <el-date-picker
            clearable
            v-model="form.paymentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择付款日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="付款单位" prop="paymentUnit">
          <el-input v-model="form.paymentUnit" placeholder="请输入付款单位" />
        </el-form-item>
        <el-form-item label="付款方式" prop="paymentMethod">
          <el-select v-model="form.paymentMethod" placeholder="请选择付款方式">
            <el-option
              v-for="dict in dict.type.bs_payment_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="付款金额" prop="paymentMoney">
          <el-input v-model="form.paymentMoney" placeholder="请输入付款金额" />
        </el-form-item>

        <el-form-item label="收款单位" prop="supName">
          <el-input v-model="form.supName" placeholder="请输入收款单位" />
        </el-form-item>

        <el-form-item label="违约责任" prop="backOut">
          <el-input v-model="form.backOut" placeholder="请输入违约责任" />
        </el-form-item>
        <el-form-item label="付款凭证" prop="paymentFileName">
          <ImageUpload></ImageUpload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAgree,
  getAgree,
  delAgree,
  addAgree,
  updateAgree,
} from "@/api/basic/agree";
import { getPlanInfoListByBindCode } from "@/api/purs/planInfo";
export default {
  dicts: ["bs_payment_method", "bs_payment_type"],
  data() {
    return {
      bindCode: "",
      planInfoList: [],
      planInfoTotal: 0,
      planInfoqueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同约定表格数据
      agreeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        paymentType: [
          { required: true, message: "款项类型不能为空", trigger: "change" },
        ],
        paymentTime: [
          { required: true, message: "付款日期不能为空", trigger: "blur" },
        ],
        paymentUnit: [
          { required: true, message: "付款单位不能为空", trigger: "blur" },
        ],
        supCode: [
          { required: true, message: "供应商编号不能为空", trigger: "blur" },
        ],
        supName: [
          { required: true, message: "收款单位不能为空", trigger: "blur" },
        ],
        paymentMethod: [
          { required: true, message: "付款方式不能为空", trigger: "change" },
        ],
        paymentMoney: [
          { required: true, message: "付款金额不能为空", trigger: "blur" },
        ],
        backOut: [
          { required: true, message: "违约责任不能为空", trigger: "blur" },
        ],
        contractCode: [
          { required: true, message: "合同编号不能为空", trigger: "blur" },
        ],
        paymentFilePath: [
          { required: true, message: "付款凭证路径不能为空", trigger: "blur" },
        ],
        paymentFileName: [
          { required: true, message: "付款凭证不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getPlanInfoList();
    this.getList();
  },
  methods: {
    getPlanInfoList() {
      getPlanInfoListByBindCode(this.bindCode).then((res) => {
        this.planInfoTotal = res.total;
        this.planInfoList = res.rows;
      });
    },

    /** 查询合同约定列表 */
    getList() {
      this.loading = true;
      listAgree(this.queryParams).then((response) => {
        this.agreeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        agreeId: null,
        paymentType: null,
        paymentTime: null,
        paymentUnit: null,
        supCode: null,
        supName: null,
        paymentMethod: null,
        paymentMoney: null,
        backOut: null,
        contractCode: null,
        paymentFilePath: null,
        paymentFileName: null,
        createTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.agreeId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同约定";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const agreeId = row.agreeId || this.ids;
      getAgree(agreeId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合同约定";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.agreeId != null) {
            updateAgree(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAgree(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const agreeIds = row.agreeId || this.ids;
      this.$modal
        .confirm('是否确认删除合同约定编号为"' + agreeIds + '"的数据项？')
        .then(function () {
          return delAgree(agreeIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "basic/agree/export",
        {
          ...this.queryParams,
        },
        `agree_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scope>
.contract-title {
  width: 1200px;
  height: 120px;
  border: 1px solid red;
  margin: 0 auto;
}

.add-contract-box {
  width: 100%;
  height: 1700px;
  background-color: #f1f4f8;
  position: relative;
}

.contract-head {
  width: 100%;
  height: 60px;
  box-sizing: border-box;
  background-color: #ffffff;
  border: 1px solid #ccc;
  line-height: 60px;
  font-family: "宋体";
  font-weight: bold;
  font-style: italic;
  text-indent: 68px;
  background-image: url("../../../image/我的计划.png");
  background-repeat: no-repeat;
  background-size: 30px 30px;
  background-position: 30px center;
}

.box-contract-info {
  width: 1300px;
  height: 450px;
  border: 1px solid #ccc;
  background-color: #ffffff;
  margin: 20px auto;
  border-radius: 18px;
}

.box-product-info {
  width: 1300px;
  overflow: auto;
  border: 1px solid #ccc;
  background-color: #ffffff;
  margin: 20px auto;
  border-radius: 18px;
}

.box-product-info > div:nth-of-type(1) {
  width: 100px;
  height: 40px;
  line-height: 40px;
  text-indent: 20px;
  font-weight: bold;
  color: black;
}

.box-bind-info {
  width: 1300px;
  height: 270px;
  border: 1px solid #ccc;
  background-color: #ffffff;
  margin: 20px auto;
  border-radius: 18px;
}

.box-bind-info > div:nth-of-type(1) {
  width: 100px;
  height: 40px;
  line-height: 40px;
  text-indent: 20px;
  font-weight: bold;
  color: black;
}

.box-contract-info > div:nth-of-type(1) {
  width: 100px;
  height: 40px;
  line-height: 40px;
  text-indent: 20px;
  font-weight: bold;
  color: black;
}

.basic-info {
  width: 400px;
  height: 40px;
  margin: 20px 100px;
  float: left;
}

.time-box {
  width: 400px;
  height: 40px;
  margin: 20px 100px;
  float: left;
}

.time-box > span {
  font-family: "仿宋";
  font-weight: bold;
}

.basic-info > span {
  font-family: "仿宋";
  font-weight: bold;
}

.my_style {
  width: 200px;
  margin-left: 20px;
  border-bottom: 1px solid gray;
}

.basic-info .el-input input {
  border: none;
  outline: none;
  box-shadow: none;
  font-weight: bold;
  font-family: "仿宋";
  font-size: 15px;
}

.test {
  border: 1px;
  outline: 1px;
  box-shadow: 1px;
}

.textarea-sty {
  width: 800px;
  position: absolute;
  left: 206px;
  border: none;
  outline: none;
  box-shadow: none;
  border: 1px solid black;
  border-radius: 5px;
}
</style>